package com.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Title: JDBCUtil
 * @Description: [JBBC 来直接访问数据库]
 * @Author lei.cheng
 * @Date 2024/12/4
 */
public class JDBCUtil {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        // 1. 加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2. 用户信息和url
        String url = "jdbc:mysql://localhost:3306/sys?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
        String username="root";
        String password="123456";
        // 3. 连接成功，数据库对象 Connection
        Connection connection = DriverManager.getConnection(url,username,password);
        // 4. 执行SQL对象Statement，执行SQL的对象
        String sql = "SELECT * FROM sys_config";
        // 5. 创建语句执行句柄
        Statement statement = connection.createStatement();
        // 6. 执行SQL对象，获取结果
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            System.out.print("variable:" + resultSet.getString("variable"));
            System.out.print(", value:" + resultSet.getString("value"));
            System.out.print(", set_time:" + resultSet.getString("set_time"));
            System.out.print(", set_by:" + resultSet.getString("set_by"));
            System.out.println();
        }
        // 7. 释放连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}